﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
//using ScadaClient.Dataset;
using System.Text.RegularExpressions;

namespace PEC.Forms
{
    public partial class FrmOptionTimeOfWeek : Form
    {
        // mang chua so ngay trong thang
        int _soBang = 1;
        private ArrayList v_arrNgayThang = new ArrayList();
        private ArrayList v_arrThang = new ArrayList();
        //private DataSet1 v_Dataset = new DataSet1();
        DataTable _dtCalendar = new DataTable();
        private string[] _ThoiGianLamViec = new string[196];
        // khai báo biến lấy dữ liệu
        private string[] v_kq;
        private string[] v_TenTo;
        public FrmOptionTimeOfWeek()
        {
            InitializeComponent();

        }
        private void setTime()//string _ThoiGianTo1, string _ThoiGianTo2, string _ThoiGianTo3, string _ThoiGianTo4, string _ThoiGianTo5, string _ThoiGianTo6, string _ThoiGianTo7, string _ThoiGianTo8
        {
            _dtCalendar.Rows.Clear();
            string[] _thu = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };// { "Chủ nhật", "Thứ 2", "Thứ 3", "Thứ 4", "Thứ 5", "Thứ 6", "Thứ 7" };
            char[] equalsign = new char[1];
            equalsign[0] = ',';

            DateTime _Now = DateTime.Now;
            //Thach sua
            _Now = _Now.AddMonths((int)cboThang.SelectedValue - _Now.Month);
            //DateTime _Now = Convert.ToDateTime(Convert.ToInt32(this.cboThang.SelectedValue) + "/01/" + DateTime.Now.Year);  // so ngay lam viec cua thang
            int _DaysInMonth = DateTime.DaysInMonth(_Now.Year, _Now.Month);
            int iThang = Convert.ToInt32(this.cboThang.SelectedValue);
            string[][] _thoigian = new string[_soBang][];
            for (int index = 0; index < _soBang; index++)
            {
                _thoigian[index] = _ThoiGianLamViec[iThang + index * 13].Split(equalsign);
            }

            v_arrNgayThang = new ArrayList();
            // lay thu cho ngay dau thang
            int _Day = Convert.ToInt32(_Now.AddDays(-_Now.Day + 1).DayOfWeek);

            for (int i = 0; i < _DaysInMonth; i++)
            {
                DataRow row = _dtCalendar.NewRow();
                row["Thu"] = _thu[_Day];
                row["Ngay"] = Convert.ToString(i + 1);
                for (int index = 0; index < _soBang; index++)
                {
                    string tmp = "0";
                    try
                    {
                        tmp = _thoigian[index][i + 1];
                    }
                    catch { }
                    row["Line" + (index + 1) + ""] = tmp;
                }

                _dtCalendar.Rows.Add(row);
                _Day++;
                if (_Day == 7)
                {
                    _Day = 0;   //0=sunday,...,6=sartuday
                }
            }

            this.Addarr();
            //dataGridViewThoiGianLamtrongthang.DataSource = v_arrNgayThang; 
        }
        // lay du lieu tu control sua ngay 06/06/2008
        private void GetTime()
        {
            for (int index = 0; index < _soBang; index++)
            {
                v_kq[index] = this.cboThang.Text;

                for (int i = 0; i < dataGridViewThoiGianLamtrongthang.Rows.Count; i++)
                {
                    DataGridViewRow dgvr = dataGridViewThoiGianLamtrongthang.Rows[i];
                    if (Convert.ToString(dgvr.Cells["Thu"].Value) != "")
                    {
                        v_kq[index] += "," + Convert.ToInt32(dgvr.Cells["Line" + (index + 1) + ""].Value);
                    }
                }
            }
        }
        private void SetDataCombobox()
        {
            string[] _thang = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("ID", typeof(int)), new DataColumn("Ten") });
            for (int i = 0; i < _thang.Length; i++)
            {
                dt.Rows.Add(i + 1, _thang[i]);
            }
            this.cboThang.DataSource = dt;
            
        }
        private void FormTimeOfWeek_Load(object sender, EventArgs e)
        {
            //_soBang = Convert.ToInt32(Classes.frmMain.__config.ScadaSetting.Rows[0][Classes.frmMain.__config.ScadaSetting.SoBangColumn]);
            _ThoiGianLamViec = new String[_soBang * 13 + 1];
            //v_kq = new string[_soBang];
            int index = 0;
            v_TenTo = new string[_soBang];
            //ScadaClient.clsIOStreams f = new ScadaClient.clsIOStreams();
            //for (int index = 0; index < _soBang; index++)
            //{
            v_TenTo[index] = Convert.ToString(index + 1);
            //}
            //_ThoiGianLamViec = Classes.frmMain.__config.Calendar.Rows[0]["Data"].ToString().Split('\n');
            //if(Classes.frmMain.__config.Calendar.Rows[0]["Data"].ToString()=="")
            for (int line = 0; line < _ThoiGianLamViec.Length; line++)
            {
                _ThoiGianLamViec[line] = "";
            }
            //else _ThoiGianLamViec = Classes.frmMain.__config.Calendar.Rows[0]["Data"].ToString().Split('\n');
            this.ColumnsParent();
            InitDataTabel();
            this.SetDataCombobox();
            this.cboThang.SelectedValue = DateTime.Now.Month;
        }

        private void InitDataTabel()
        {
            DataColumn[] colheader = new DataColumn[] { new DataColumn("Thu"), new DataColumn("Ngay") };
            _dtCalendar.Columns.AddRange(colheader);
            for (int index = 0; index < _soBang; index++)
            {
                DataColumn col = new DataColumn("Line" + (index + 1) + "");
                col.Caption = "Line " + (index + 1) + "";
                _dtCalendar.Columns.Add(col);
            }
        }
        private void KienTra()
        {

        }
        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetTime();
                int _Thang = Convert.ToInt32(this.cboThang.SelectedValue);
                for (int index = 0; index < _soBang; index++)
                {
                    _ThoiGianLamViec[_Thang + (13 * index)] = v_kq[index];
                    if (index > 0)
                        _ThoiGianLamViec[index * 13] = "[Line" + (index + 1) + "]";
                }
                //if (Classes.frmMain.__config.Calendar.Rows.Count == 0)
                //    Classes.frmMain.__config.Calendar.AddCalendarRow("");
                //Classes.frmMain.__config.Calendar.Rows[0]["Data"] = string.Join("\n", _ThoiGianLamViec);
                //Classes.frmMain.SaveConfig();
                MessageBox.Show("Save successful");
            }
            catch
            {
                MessageBox.Show("Save failure, please try again or contact to administrator");
            }
        }
        private void cboThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _Thang = Convert.ToInt32(this.cboThang.SelectedValue);
            this.setTime();//_ThoiGianLamViec[_Thang], _ThoiGianLamViec[_Thang + 13], _ThoiGianLamViec[_Thang + 26], _ThoiGianLamViec[_Thang + 39], _ThoiGianLamViec[_Thang + 52], _ThoiGianLamViec[_Thang + 65], _ThoiGianLamViec[_Thang + 78], _ThoiGianLamViec[_Thang + 91]
        }
        private void ColumnsParent()
        {
            //DataGridViewTextBoxColumn[] Columns = new DataGridViewTextBoxColumn[8];

            DataGridViewTextBoxColumn Columns = SetFunctionsColumn("Day of Week", "Thu", 100, true);
            Columns.DefaultCellStyle.Format = "00.#";
            dataGridViewThoiGianLamtrongthang.Columns.AddRange(Columns);
            Columns = SetFunctionsColumn("Day", "Ngay", 100, true);
            Columns.DefaultCellStyle.Format = "00.#";
            dataGridViewThoiGianLamtrongthang.Columns.AddRange(Columns);
            for (int index = 0; index < _soBang; index++)
            {
                Columns = SetFunctionsColumn("Line" + v_TenTo[index], "Line" + (index + 1), 100, true);
                dataGridViewThoiGianLamtrongthang.Columns.AddRange(Columns);
            }
            //return Columns;
        }
        private DataGridViewTextBoxColumn SetFunctionsColumn(string HeaderText, string DataPropertyName, int Width, Boolean Visible)
        {
            DataGridViewTextBoxColumn Column = new DataGridViewTextBoxColumn();
            Column.Name = DataPropertyName;
            Column.HeaderText = HeaderText;
            Column.DataPropertyName = DataPropertyName;
            Column.Width = Width;
            Column.Visible = Visible;
            return Column;
        }
        private void Addarr()
        {
            dataGridViewThoiGianLamtrongthang.Columns.Clear();
            this.ColumnsParent();
            dataGridViewThoiGianLamtrongthang.DataSource = _dtCalendar;
            for (int idx = 0; idx < dataGridViewThoiGianLamtrongthang.Rows.Count; idx++)
            {
                if (dataGridViewThoiGianLamtrongthang[0, idx].Value == "Sun" || dataGridViewThoiGianLamtrongthang[0, idx].Value == "Sat")
                {
                    DataGridViewCellStyle a = new DataGridViewCellStyle();
                    a.Font = new Font(this.Font, FontStyle.Bold);
                    a.ForeColor = Color.BlueViolet;
                    dataGridViewThoiGianLamtrongthang.Rows[idx].DefaultCellStyle = a;
                }
            }
        }

        private void btnSetAll_Click(object sender, EventArgs e)
        {
            if (Regex.IsMatch(txtTime.Text, @"^[0-9]+$"))
            {
                foreach (DataRow row in _dtCalendar.Rows)
                {
                    for (int index = 0; index < row.Table.Columns.Count - 2; index++)
                    {
                        if (row["Thu"].ToString() != "Chủ nhật" && row["Thu"].ToString() != "Sun")
                            row["Line" + (index + 1) + ""] = txtTime.Text;
                        else
                            row["Line" + (index + 1) + ""] = "0";
                    }
                }
            }
        }

        private void dataGridViewThoiGianLamtrongthang_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {

        }

        private void dataGridViewThoiGianLamtrongthang_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {

        }
    }
}